package com.yanqu.road.server.manger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

/**
 * @Author wpj
 * @Date 2021/10/9
 */
public class ForbiddenIpMgr extends TempMgr {
    // 消息队列
    private static Queue<Object> msgQueue = new LinkedList<Object>();

    private static final int maxSize = 9999;

    private static boolean OPEN = false;
    private static String UPLOAD_URL = "";

    public static void add(Object msg) {
        getLogger().info("{}  {} {}", OPEN, UPLOAD_URL, (String) msg);
        try {
            if (OPEN) {
                synchronized (msgQueue) {
                    msgQueue.add(msg);
                    checkQueue();
                }
            }
        } catch (Exception ex) {

        }

    }

    public static void upload() {
        try {
            JSONObject jsonObject=new JSONObject();
            Map<String, String> headerMap = new HashMap<>();
            headerMap.put("Content-Type", "application/json");
            JSONArray jsonArray = poll(maxSize);
            if (!jsonArray.isEmpty()) {
                jsonObject.put("ipList",jsonArray);
                String result = HttpHelper.doPostJsonTimeOut(UPLOAD_URL, jsonObject.toString(), "UTF-8", headerMap);
            }

        } catch (Exception e) {
            getLogger().error(e.getMessage());
        } finally {

        }

    }

    public static void checkQueue() {
        int queueSize = msgQueue.size();
        if (queueSize >= maxSize) {
            JSONArray jsonArray = poll(queueSize - maxSize);

        }
    }

    public static JSONArray poll(int max_num) {
        JSONArray jsonArray = new JSONArray();
        synchronized (msgQueue) {
            for (int i = 0; i < max_num; i++) {
                Object data = msgQueue.poll();
                if (data == null) {
                    break;
                }
                jsonArray.add(data);
            }
            return jsonArray;
        }
    }

    private static Map<String, Long> forbiddenIpMap; // 封禁的IP列表

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        forbiddenIpMap = ServerBussiness.getForbiddenIpMap();

        String[] ss = ChannelConfig.FORBIDDEN_IP_PARAM.split("\\|");
        if (ss[0].equals("1")) {
            OPEN = true;
            UPLOAD_URL = ss[1];
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
     * 是否被封禁的IP
     * @param ip
     * @return
     */
    public static boolean isForbiddenIp(String ip) {
        if (StringUtils.isNullOrEmpty(ip)) {
            return false;
        }

        if (forbiddenIpMap.containsKey(ip)) {
            long forbiddenEndTime = forbiddenIpMap.get(ip);
            return forbiddenEndTime < System.currentTimeMillis(); // 封禁时间未到
        }
        return false;
    }

    public static void reloadForbiddenIp() {
        forbiddenIpMap = ServerBussiness.getForbiddenIpMap();
    }

    public static Map<String, Long> getForbiddenIpMap() {
        return forbiddenIpMap;
    }
}
